﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Collections;
using System.IO;
using System.Threading;
using System.Runtime.InteropServices;
using DeviceSDK.LED;
using BaseHelper;
using DeviceSDK;
using BaseHelper.Base;

namespace DeviceSDK.LED.OnBon
{
    /// <summary>
    ///仰邦接收卡
    /// </summary>
    public class OnBon_Y
    {
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        private delegate void SendProgramHandler();



        private static bool IsInit = false;
        private static object locker = new object();
        private static ushort base_port = 80;
        private static string base_user = "guest";
        private static string base_pass = "guest";

         

        public static string Get_Error(int err)
        {
            switch (err)
            {
                case 0:
                    return "操作成功！";

                case 1:
                    return "操作失败！";

                case 2:
                    return "发送节目单失败！";

                case 3:
                    return "保存节目单到磁盘失败！";

                case 4:
                    return "有素材文件格式不支持！";

                case -1:
                    return "操作失败！";

                default:
                    return "未知错误！";
            }
        }

        //public static int Get_Status(string IP)
        //{
        //    //2019.6.24 
        //    //LedYNetSdk.Init();
        //    Init();
        //    short w = 0;
        //    short h = 0;
        //    ushort type = 0;
        //    int err=  LedYNetSdk.Net_GetScreeninfo(IP, ref type, ref w, ref h);
        //    //LedYNetSdk.Release();
        //    return err;
        //}


        public static void Init()
        {
            lock (locker)
            {
                if (!IsInit)
                {
                    LedYNetSdk.init_sdk();
                    Application.DoEvents();
                    IsInit = true;
                }
            }
        }

        //发送-节目
        public static int Send_Programs(string IP, int Width, int Height, OnBon_ProductID Pid, PlaybillProgramInfo[] Programs)
        {
            Init();

            int ret = 0;
            int err=0;
            byte[] ip = Encoding.ASCII.GetBytes(IP);
            try
            {
            
                lock (locker)
                {
    
                    IntPtr playlist = LedYNetSdk.create_playlist(Width,Height , (int)Pid);
                    IntPtr program = LedYNetSdk.create_program("program_0", "0xff000000");
            

                    foreach (PlaybillProgramInfo pinfo in Programs)
                    {
                        pinfo.IsEffectRand = pinfo.IsEffectRand == 1 ? 1 : 2;
                        switch (pinfo.Type)
                        {
                            case "picture":
                                if (!File.Exists(pinfo.FilePath))
                                    continue;
                                
                                
                                IntPtr pic_area = LedYNetSdk.create_pic();
                            
                                err = LedYNetSdk.add_pic_unit(pic_area,pinfo.PlayTime,pinfo.IsEffectRand,pinfo.EffectTime,pinfo.FilePath);
                                err = LedYNetSdk.add_pic(program, pic_area,pinfo.X,pinfo.Y,pinfo.Width,pinfo.Height,0);
                                
                                break;

                            case "video":
                                if (!File.Exists(pinfo.FilePath))
                                    continue;
                                IntPtr video_area = LedYNetSdk.create_video();
                                err=LedYNetSdk.add_video_unit(video_area, 0,1,0,pinfo.PlayTime,pinfo.FilePath);
                                err=LedYNetSdk.add_video(program, video_area, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height, 0, 0, 0, "");

                                break;

                            case "rtf":
                                
                                break;

                            case "text":
                                //pinfo.FilePath = RT.Service.Release.Helper.TextToImage.CreateImage(pinfo.Text, pinfo.Width, pinfo.Height, pinfo.FontName, pinfo.FontSize, pinfo.FontColor);
                                //pb.AddImage(pinfo.FilePath, pinfo.PlayTime*100, pinfo.X, pinfo.Y, pinfo.Width, pinfo.Height);

                                pinfo.Text = pinfo.Text.Replace("\n", "\r\n").Replace("\r\r","\r");
                                IntPtr area_tree = LedYNetSdk.create_text();
                                //水平居左
                                //水平居右
                                //水平居中
                                //垂直居上
                                //垂直居下
                                //垂直居中

                                err = LedYNetSdk.add_text_unit_text(area_tree, pinfo.PlayTime, pinfo.IsEffectRand, pinfo.FontName, pinfo.FontSize,pinfo.FontIsBold?"bold":"" , "", pinfo.FontColor, "0xff000000", pinfo.Text);
                                err = LedYNetSdk.add_text(program, area_tree,pinfo.X,pinfo.Y,pinfo.Width,pinfo.Height,255,0,1);
                                break;
                        }


                    }

                    err = LedYNetSdk.add_program_in_playlist(playlist, program,1,1,"","","","",255);
               

                    string tempFolder = LEDHelper.TempFullPath +IP.Replace(".", "_") + "\\";
                    if (!Directory.Exists(tempFolder))
                        Directory.CreateDirectory(tempFolder);

                    try
                    {
                      
                        long free_size = 0; long total_size = 0;
                        ret = LedYNetSdk.send_program(ip,base_port,base_user,base_pass ,tempFolder, playlist, 0, ref free_size, ref total_size);
                        LedYNetSdk.cancel_send_program(playlist);
                        LedYNetSdk.delete_playlist(playlist);
                        if (ret == 0)
                            return 0;
                        else
                            return 2;

                    }
                    catch (Exception ex2)
                    {
                        LogHelper.Err("DeviceSDK.LED.OnBon.OnBon_Y.cs", "SendProgram(ex2," + IP + ")", ex2);
                    }
                }//locker

            }
            catch (Exception ex)
            {
               LogHelper.Err("DeviceSDK.LED.OnBon.OnBon_Y.cs", "SendProgram(ex," + IP + ")", ex);
            }

            return ret;
        }

                
        #region 设置

        //设置-屏幕-关闭
        public static int Set_Screen_Close(string IP)
        {
            Init();
            byte[] ip = Encoding.ASCII.GetBytes(IP);

            ///LedYNetSdk.Init();
            int ret= LedYNetSdk.set_screen_turnonoff(ip,base_port, base_user,base_pass, 0);
            //LedYNetSdk.Release();

            return ret;
        }

        //设置-屏幕-开启
        public static int Set_Screen_Open(string IP)
        {
            Init();
            byte[] ip = Encoding.ASCII.GetBytes(IP);
            int ret = LedYNetSdk.set_screen_turnonoff(ip, base_port, base_user, base_pass, 1);
            return ret;
        }
        
        //设置-屏幕亮度
        public static int Set_Brightness(string IP, int Value)
        {
            Init();
            byte[] ip = Encoding.ASCII.GetBytes(IP);
            int ret = LedYNetSdk.set_screen_brightness(ip,base_port,base_user,base_pass,Value);
            return ret;
        }


        //设置-音量
        public static int Set_Volume(string IP, int Value)
        {
            Init();
            byte[] ip = Encoding.ASCII.GetBytes(IP);
            int ret = LedYNetSdk.set_screen_volumn(ip,base_port,base_user,base_pass,Value);
            return ret;
        }

        //设置-时间
        public static int Set_Time(string IP)
        {
            Init();
            byte[] ip = Encoding.ASCII.GetBytes(IP);
            int ret = LedYNetSdk.check_time(ip,base_port,base_user,base_pass);
            return ret;

        }



        //屏幕截图
        public static int Get_ScreenCap(string IP,int Width,int Height, ref Image Img)
        {
            Init();
            byte[] ip = Encoding.ASCII.GetBytes(IP);
            string path=LEDHelper.TempFullPath + IP.Replace(".","_") +"_capscreen.png";
            int ret=LedYNetSdk.get_screen_capture(ip,(ushort)base_port,base_user,base_pass,path, Width,Height);
            if (ret==0 && File.Exists(path))
            { 

                
            }
            return ret;
        }

        #endregion



    }
}
